package cn.com.yto56.cainiao.exponent.elasticsearch.plugin;

import cn.com.yto56.basic.framework.core.util.DateUtil;
import cn.com.yto56.basic.framework.core.util.EmptyUtils;
import cn.com.yto56.basic.framework.model.rest.MutiRequest;
import cn.com.yto56.basic.framework.plugin.chain.config.ExecutorRequest;
import cn.com.yto56.basic.framework.plugin.elasticsearch.TagRequest;
import cn.com.yto56.cainiao.exponent.dto.ExpUploadRequest;
import cn.com.yto56.cainiao.exponent.dto.ExponentRequest;
import cn.com.yto56.cainiao.exponent.plugin.ExponentConvertPlugin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
@Slf4j
@Order(1)
public class AccuracyConvertPlugIn extends ExponentConvertPlugin<ExpUploadRequest, MutiRequest<ExponentRequest>> {


    /**
     * 扩展类型
     * @return
     */
    @Override
    protected String extentionName(){
        return "信息准确率";
    }


    /**
     * 根据参数判断是否可以执行该插件
     * @param parameter
     * @return
     */
    @Override
    protected Boolean allowExecute(ExecutorRequest<ExpUploadRequest,MutiRequest<ExponentRequest>> parameter){
        return  EmptyUtils.isNotEmpty(parameter)
                &&
                EmptyUtils.isNotEmpty(parameter.getInitParameter())
                &&
                parameter.getInitParameter().getFileName().startsWith(extentionName())
                &&
                EmptyUtils.isNotEmpty(parameter.getInitParameter().getRowValues()
                ) ;
    }


    @Override
    protected MutiRequest<ExponentRequest> convert(ExecutorRequest<ExpUploadRequest, MutiRequest<ExponentRequest>> parameter) {
        parameter.getInitParameter().getRowValues().stream().forEach(n->{
            ExponentRequest exponentRequest = new ExponentRequest();
            exponentRequest.setExtProperties(new ArrayList<>());
            exponentRequest.setConsumerTime(new Date());
            exponentRequest.setExponentType(extentionName());
            exponentRequest.setPullDate(DateUtil.toDate(  parameter.getInitParameter().getDateFmt()));
            exponentRequest.setWayBillNo(n.get("运单号"));
            exponentRequest.getExtProperties().add(new TagRequest().setTagCode(n.get("信息类异常类型")).setTagString(n.get("信息类异常类型")));
            exponentRequest.setExponetMsg(n.get("信息类异常类型"));
            exponentRequest.getExtProperties().add(new TagRequest().setTagCode(parameter.getInitParameter().getArea()));
            parameter.getOutputParameter().getItems().add(exponentRequest);
        });
        return parameter.getOutputParameter();
    }
}
